{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import genfromtxt\n",
    "from sklearn import linear_model\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  83.     234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [  88.5    259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [  88.2    258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [  89.5    284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [  96.2    328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [  98.1    346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [  99.     365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 100.     363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 101.2    397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 104.6    419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 108.4    442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 110.8    444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 112.6    482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 114.2    502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 115.7    518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 116.9    554.894  400.7    282.7    130.081 1962.      70.551]]\n"
     ]
    }
   ],
   "source": [
    "#读入数据\n",
    "data = genfromtxt(\"longley.csv\",delimiter = ',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 234.289  235.6    159.     107.608 1947.      60.323]\n",
      " [ 259.426  232.5    145.6    108.632 1948.      61.122]\n",
      " [ 258.054  368.2    161.6    109.773 1949.      60.171]\n",
      " [ 284.599  335.1    165.     110.929 1950.      61.187]\n",
      " [ 328.975  209.9    309.9    112.075 1951.      63.221]\n",
      " [ 346.999  193.2    359.4    113.27  1952.      63.639]\n",
      " [ 365.385  187.     354.7    115.094 1953.      64.989]\n",
      " [ 363.112  357.8    335.     116.219 1954.      63.761]\n",
      " [ 397.469  290.4    304.8    117.388 1955.      66.019]\n",
      " [ 419.18   282.2    285.7    118.734 1956.      67.857]\n",
      " [ 442.769  293.6    279.8    120.445 1957.      68.169]\n",
      " [ 444.546  468.1    263.7    121.95  1958.      66.513]\n",
      " [ 482.704  381.3    255.2    123.366 1959.      68.655]\n",
      " [ 502.601  393.1    251.4    125.368 1960.      69.564]\n",
      " [ 518.173  480.6    257.2    127.852 1961.      69.331]\n",
      " [ 554.894  400.7    282.7    130.081 1962.      70.551]]\n",
      "[ 83.   88.5  88.2  89.5  96.2  98.1  99.  100.  101.2 104.6 108.4 110.8\n",
      " 112.6 114.2 115.7 116.9]\n"
     ]
    }
   ],
   "source": [
    "#切分数据\n",
    "x_data = data[:,1:]\n",
    "y_data = data[:,0]\n",
    "print(x_data)\n",
    "print(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.03464209711722\n",
      "[0.10206856 0.00409161 0.00354815 0.         0.         0.        ]\n"
     ]
    }
   ],
   "source": [
    "#Lasso模型\n",
    "model = linear_model.LassoCV()\n",
    "#弹性网模型\n",
    "# model = linear_model.ElasticNetCV()\n",
    "model.fit(x_data,y_data)\n",
    "\n",
    "#Lasso系数\n",
    "print(model.alpha_)\n",
    "#相关系数\n",
    "print(model.coef_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([115.6461414])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(x_data[-2,np.newaxis])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
